package com.kx.center.task;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.kx.center.common.Constant;
import com.kx.center.common.CustomHashMap;
import com.kx.center.entity.shop.ShopOrder;
import com.kx.center.entity.shop.ShopOrderPay;
import com.kx.center.service.ShopOrderService;
import com.kx.center.util.JSONParser;
import com.kx.center.util.MD5Coder;
import com.kx.center.util.SerialNumber;
import com.kx.center.util.WPosUtil;
import com.kx.center.util.WXUtil;

@Service
public class QueryPayResultTask {

	public static void main(String[] args) throws Exception {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		QueryPayResultTask tt = ac.getBean(QueryPayResultTask.class);
		tt.run();
	}

	@Autowired
	ShopOrderService shopOrderService;

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void run() throws Exception {

		List<ShopOrderPay> pns = shopOrderService.findShopOrderPayPN();
		for (ShopOrderPay pn : pns) {
			if(pn.getType() == 1){
				String json = WPosUtil.getPaymentInfo(pn.getPayNum());
				HashMap<String, Object> hashmap = (HashMap<String, Object>) JSONParser
						.parse(json, new HashMap<String, Object>().getClass());
				CustomHashMap<String, Object> item = CustomHashMap.convert(hashmap);
				if (item.getInt("errCode") != 0) {

				} else {
					ShopOrder shopOrder = new ShopOrder();
					shopOrder.setPayNum(item.getString("out_trade_no"));
					shopOrder.setOrderStatus(item.getString("trade_status").equals(
							"PAY") ? 4 : 5);
					shopOrder.setId(item.getInteger("attach"));
					shopOrderService.paymentShopOrder(shopOrder);

					ShopOrderPay shopOrderPay = new ShopOrderPay();
					shopOrderPay.setPayNum(item.getString("out_trade_no"));
					shopOrderPay.setPayResult(json);
					shopOrderPay.setType(item.getInt("pay_type"));
					shopOrderPay.setStatus(item.getString("trade_status").equals(
							"PAY") ? 2 : 3);
					shopOrderService.updatePayStatusByPayNum(shopOrderPay);
				}
			}else if(pn.getType() == 3){
				
				String params, sign, xml, result;

				HashMap<String, String> map = new HashMap<String, String>();

				map.put("appid", Constant.appid_wx);
				map.put("mch_id", Constant.mch_id_wx);
				map.put("nonce_str", SerialNumber.getRandomString(16));
				map.put("out_trade_no", pn.getPayNum());

				params = WXUtil.concatParams(map) + "&key=" + Constant.key_wx + "";
				sign = MD5Coder.encode(params);
				map.put("sign", sign.toUpperCase());

				xml = WXUtil.mapToXml(map, "xml");
				result = WXUtil.post(Constant.query_url_wx, xml);
				HashMap<String, Object> maps = WXUtil.xmlToMap(result);
				CustomHashMap<String, Object> items = CustomHashMap.convert(maps);
				if (items.getString("result_code").equals("FAIL")) {

				} else {
					ShopOrder shopOrder = new ShopOrder();
					shopOrder.setPayNum(items.getString("out_trade_no"));
					shopOrder.setOrderStatus(items.getString("trade_state").equals(
							"SUCCESS") ? 4 : 5);
					shopOrder.setId(items.getInteger("attach"));
					shopOrderService.paymentShopOrder(shopOrder);

					ShopOrderPay shopOrderPay = new ShopOrderPay();
					shopOrderPay.setPayNum(items.getString("out_trade_no"));
					shopOrderPay.setPayResult(items.toString());
					shopOrderPay.setType(3);
					shopOrderPay.setStatus(items.getString("trade_state").equals(
							"SUCCESS") ? 2 : 3);
					shopOrderService.updatePayStatusByPayNum(shopOrderPay);
				}
			}else{
				return;
			}
		}
	}

}
